/**
 * http://www.taxad.cn/news/newscat.aspx?catid=2
 * 税务培训网法规库爬虫
 * sunny 2017-06-14
 */

// 工具类
const utils = require('../../utils');

import { Base, Item } from "../base";

// name:whxy001
// pass:123456
// logauto:0
const tax_types: any = {
    "增值税": 1,
    "营业税": 2,
    "消费税": 3,
    "企业所得税": 4,
    "进出口税": 5,
    "个人所得税": 6,
    "资源税": 7,
    "城镇土地使用税": 8,
    "城市维护建设税": 9,
    "土地增值税": 10,
    "房产税": 11,
    "车船税": 12,
    "车辆购置税": 13,
    "印花税": 14,
    "耕地占用税": 15,
    "契税": 16,
    "烟叶税": 17,
    "船舶吨税": 21
};

class taxlaws_spider extends Base {
    tick: number;
    logined: boolean;

    constructor() {
        super();
        this.name = "税务培训网法规库";
        this.encode = "utf-8"; // 爬取页面的编码，默认为-8
        this.enabled = true;
        this.tick = 0;
        this.logined = false;
        this.collection = "laws";
    }

    getPageCount(): number {
        this.login();
        return 18;
    }

    getItems(pageIndex: number): Item[] {
        var target: string = "", i: number = 0;
        for (var k in tax_types) {
            if (i == pageIndex) {
                target = k;
                break;
            }
            i++;
        }
        var self = this;
        console.log(self.name + " " + self.version + " start...");
        if (pageIndex == 0) {
            var l = self.login();
            if (l == "true") {
                self.logined = true;
            } else {
                self.logined = false;
            }
        }
        var index = tax_types[target];
        return self.getTaxType(target, index);
    }

    private login(): string {
        var self = this;
        self.spider.getSync("http://www.taxad.cn/");
        return self.spider.postSync("http://www.taxad.cn/include/ChkLogin.aspx?name=whxy001&pass=123456&logauto=0", {});
    }

    private getTaxType(t: string, index: number): Item[] {
        var self = this;
        var items: Item[] = [];
        var url = "http://www.taxad.cn/news/newstsort.aspx?catid=" + index;
        var content = self.spider.getSync(url);
        var $ = self.cheerio.load(content);
        var cnt = $(".pagesbox").text().trim(); //共 95 个/
        var count = 0;
        if (/共 (\d+) 个/.test(cnt)) {
            count = parseInt(RegExp.$1);
            count = Math.round((count + 14) / 15);
        }
        console.log(t + " " + count);
        for (var i = 1, j = count; i <= j; i++) {
            // utils.sleep(200);
            var _url = "http://www.taxad.cn/news/newstsort.aspx?catid=" + index + "&page=" + i;
            var _content;
            if (i == 1) _content = content;
            else _content = self.spider.getSync(_url);
            // console.log(i + "=>" + _content);
            $ = self.cheerio.load(_content);
            $("li a[target='_blank']").each(function (index, ele) {
                var $e = $(ele);
                var _href = "http://www.taxad.cn" + $e.attr("href").trim();
                if (/news\/detail/.test(_href)) {
                    // console.log(_href);
                    var _title = $e.text().trim();
                    var item = self.buildItem();
                    item.title = _title;
                    (item as any).url = _href;
                    (item as any)["类型"] = t;
                    (item as any)["网页"] = self.spider.getSync(_href);
                    items.push(item);
                    self.tick = self.tick + 1;
                    console.log(self.tick + " " + _title + " added");
                }
            });
        }
        return items;
    };
}

export = new taxlaws_spider();